package com.networkmanagerj
{
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.events.ChannelFaultEvent;
	import mx.messaging.messages.ErrorMessage;
	import mx.messaging.messages.RemotingMessage;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
   
	 
   
	public class RemoteObjectService implements IDispose
	{	 	

		
		private   var remoteMessage : RemotingMessage;		
		protected var remoteObject  : RemoteObject;
		
		private function createRemoteObject(remoteServiceId : String) : void
		{
			this.remoteObject = new RemoteObject();
			this.remoteObject.destination = remoteServiceId;
			this.remoteObject.showBusyCursor = true;
            this.remoteObject.addEventListener("result",this.resultHandler);
            this.remoteObject.addEventListener("fault",this.faultHandler);			
		}
		
		protected function resultHandler(event : ResultEvent):void
		{
		
		
			this.remoteMessage = RemotingMessage(event.token.message);
			Alert.show(remoteMessage.destination+" "+remoteMessage.operation);
			var remoteServiceEvent : XvicRemoteServiceEvents = new XvicRemoteServiceEvents(XvicRemoteServiceEvents.SUCESS,true,true);
			remoteServiceEvent.remoteOperationName = this.remoteMessage.operation;
			remoteServiceEvent.returnObject = event.result;
			
						
		    if(this.dispatchEventFunction != null)
		       this.dispatchEventFunction(remoteServiceEvent);		    
		    else   
				XvicEventDispatcher.getInstance().dispatchEvent(remoteServiceEvent);
		
		}		
		
		protected function faultHandler(event:FaultEvent):void 
		{	
			this.remoteMessage = RemotingMessage(event.token.message);
			
			var errorObject : ErrorMessage = event.message as ErrorMessage;
			var errorMessage : String;
			if(errorObject != null)
			   if(errorObject.rootCause is ChannelFaultEvent)
			      errorMessage = ChannelFaultEvent(errorObject.rootCause).faultCode;
			else
			   errorMessage = event.fault.faultCode;
			
			var remoteServiceEvent : XvicRemoteServiceEvents = new XvicRemoteServiceEvents(XvicRemoteServiceEvents.FAILED,true,true);
			remoteServiceEvent.errorMessage = errorMessage;
			remoteServiceEvent.remoteOperationName = this.remoteMessage.operation;
		
			
		    if(this.dispatchEventFunction != null)
		       this.dispatchEventFunction(remoteServiceEvent);
		    else   
				XvicEventDispatcher.getInstance().dispatchEvent(remoteServiceEvent);
		}		
		
		public function RemoteObjectService(remoteServiceId : String)
		{
			super();
			if(remoteServiceId == "" || remoteServiceId == null)
			   throw new Error("RemoteObjectService Error\nRemoteServiceId is empty");
			   
			this.createRemoteObject(remoteServiceId);		
			
		}

		public function dispose():void
		{			

			
            this.remoteObject.removeEventListener("result",this.resultHandler);
            this.remoteObject.removeEventListener("fault",this.faultHandler);
            this.remoteObject.disconnect();
			this.remoteObject = null;			
		}	
		
	
		public var dispatchEventFunction : Function = function(event:Event):void;	
	}
}

 

